#include "BleServerConnector.h"
#include "common.h"
#include <Arduino.h>

#ifndef _BleManager_H
#define _BleManager_H

#define BLE_COUNT_MAX 16 // 允许的BLE设备总数

// BLE 主机
class BleManager : public NimBLEScanCallbacks {

private:
    // 所有蓝牙设备
    static BleServerConnector s_bleConnectors[BLE_COUNT_MAX]; // 连接池

    static void backend(void *pvParameters);

    typedef void (*setOnRecvData_t)(BLERemoteCharacteristic *_pReadReportCharacteristic, uint8_t *dataBuf, int nLen, bool isNotify);

    static void scan(void *pvParameters); // 阻塞式扫描
    bool isScan = false;

public:
    BleManager();
    void init(std::string deviceName);
    void createConnection(char address[18], uint8_t addressType, int deviceType, char *deviceName); // 创建一个连接
    void startScan();        // 开启扫描
    std::string read(int n);

    void pairDevice(BleServerConnector *pConnector);
    void removePairedDevice(BleServerConnector *connector);

    static BleServerConnector *getBleServerConnector(String address);

    friend void readConfig();
    friend BleServerConnector *getBLEAdvertisedDevices();

    // -----------------------------------------------
    // NimBLEScanCallbacks
    // -----------------------------------------------
    // 回调函数： 处理广播信息
    void onResult(const NimBLEAdvertisedDevice *advertisedDevice);
    // 回调函数： 收到蓝牙设备的数据
    static void onRecvData(BLERemoteCharacteristic *pReadReportCharacteristic, uint8_t *dataBuf, int nLen, bool isNotify);
};

extern BleManager _BleManager;

#endif